package com.coupon.constrol;

import com.coupon.pojo.Coupon;
import com.coupon.pojo.PageObject;
import com.coupon.service.CouponService;
import com.coupon.util.JsonResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author :wn
 * @program : coupon
 * @descript :
 * @create :2021-01-05 09:29
 */
@Controller
@ResponseBody
@RequestMapping("/cpn")
public class CouponController {
    @Resource
    CouponService couponService;
   @PostMapping("/add")
    public Map add(@RequestBody Coupon coupon){
       Boolean aBoolean = couponService.addCouponService(coupon);
       JsonResult jsonResult=new JsonResult();
       if (aBoolean){
           jsonResult.setCode(1);
       }else {
           jsonResult.setCode(0);
       }
       return jsonResult.getValues();
    }

    @RequestMapping("/getList")
    @ResponseBody
    public Map getList(@RequestParam("page")Integer page){
        PageObject all = couponService.findAll(page);
        List<Coupon> dataList = all.getDataList();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(dataList);
        jsonResult.setCode(1);
        return jsonResult.getValues();
    }

    @RequestMapping("/exportCpnList")
    public void exportList(HttpServletResponse response) throws IOException, ParseException {
        //查所有的用户信息列表
        PageObject all = couponService.findAll(null);
        List<Coupon> dataList = all.getDataList();

        // 新建excel 工作表
        HSSFWorkbook workbook = new HSSFWorkbook();
        //添加sheet:
        HSSFSheet getSheet = workbook.createSheet("注册会员信息表");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "序号", "名称", "描述","规则","图片","领用有效期","使用有效期"};

        // 创建第一行(表头)
        HSSFRow row = getSheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 数据行
        int rowNum = 1;
        for (Coupon u : dataList) {
            HSSFRow dataRow = getSheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(u.getName());
            dataRow.createCell(2).setCellValue(u.getDesc());
            dataRow.createCell(3).setCellValue(u.getRole());
            dataRow.createCell(4).setCellValue(u.getImg());
            dataRow.createCell(5).setCellValue(u.getCollectExpire());
            dataRow.createCell(6).setCellValue(u.getUseExpire());
            rowNum++;
        }


        String fileName = "优惠券清单.xls";
        fileName = URLEncoder.encode(fileName,"utf-8").replaceAll("\\+", "%20");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
    @RequestMapping("/update")
    public Map update(@RequestParam("cpId")int cpId,@RequestParam("use")boolean use) {
        boolean update = couponService.update(cpId,use);
       JsonResult jsonResult=new JsonResult();
       if(update) {
           jsonResult.setCode(1);
       }else{
           jsonResult.setCode(0);
       }
       return jsonResult.getValues();
    }

}
